package com.store.controller;

import com.store.service.CartService;
import com.store.util.JsonResult;
import com.store.vo.CartVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api("购物车控制层")
@RestController
@RequestMapping("/cart")
public class CartController extends BaseController{

    @DubboReference(interfaceClass = CartService.class,version = "1.0.0",timeout = 5000)
    private CartService cartService;

    @GetMapping("/getCart")
    @ApiOperation("查询购物车")
    public JsonResult getCart(HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        List<CartVO> cartList = cartService.getCartByUid(uid);
        return new JsonResult(200,cartList);
    }

    @GetMapping("/addCart")
    @ApiOperation("加入购物车")
    public JsonResult addCart(Integer id, HttpSession session){
        Integer uid = (Integer) session.getAttribute("uid");
        String username = (String)session.getAttribute("username");
        cartService.addCart(uid,username,id);
        return new JsonResult(200);
    }

    @PostMapping("/updateCart")
    @ApiOperation("更新购物车数据")
    public JsonResult updateCart(Integer cid,Integer num, HttpSession session){
        String username = (String)session.getAttribute("username");
        cartService.UpdateNumByCid(cid,username,num);
        return new JsonResult(200);
    }

    @PostMapping("/deleteCart")
    @ApiOperation("删除购物车数据")
    public JsonResult deleteCart(Integer cid){
        cartService.deleteCartByCid(cid);
        return new JsonResult(200);
    }

}
